package com.test.reactor.control;

import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.logging.Level;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.reactive.function.client.WebClient;

import com.test.reactor.config.GlobalException;
import com.test.reactor.service.WebClientService;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

@Controller
@Slf4j
public class ClientController {
	static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
	@Autowired
	WebClientService webClientService;
	 @Autowired
    Executor myServiceExecuto;
	
	@GetMapping("client/{type}")
	@ResponseBody
	public Mono<String> client(@PathVariable String type) throws Exception {
		return webClientService.clent();
	}
	@GetMapping("transaction")
	@ResponseBody
	public Mono<String> transaction() throws Exception {
		return webClientService.transaction();
	}
	@GetMapping("orderMsg")
	@ResponseBody
	public Mono<String> orderMsg() throws Exception {
		return webClientService.orderMsg();
	}
	@GetMapping("netty/{ip}/{port}")
	@ResponseBody
	public Mono<String> netty(@PathVariable String ip, @PathVariable String port) throws Exception {
		System.out.println(ip + ":" + port);
		myServiceExecuto.execute(new Runnable() {
			
			@Override
			public void run() {
				final Bootstrap bootstrap = new Bootstrap();
				bootstrap.group(eventLoopGroup);
				bootstrap.channel(NioSocketChannel.class);
				
				// 虚拟ip
				bootstrap.localAddress("192.168.91.175", 1111);
				bootstrap.option(ChannelOption.SO_REUSEADDR, true);
				bootstrap.remoteAddress(ip, Integer.parseInt(port));
				bootstrap.handler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel ch) throws Exception {
					}
				});
				ChannelFuture channelFuture = bootstrap.connect();
				channelFuture.addListener(new ChannelFutureListener() {
					public void operationComplete(ChannelFuture future)
							throws Exception {
						if (future.isSuccess()) {
							System.out.println("1---isSuccess");
						} else {
							System.out.println("1---not isSuccess" +future);
						}
					}
				});
				
			}
		});
		
		
		return Mono.just("{\"status\":200}");
	}
	public static void main(String[] args) {
		Mono<String> bodyMono = WebClient.create().get()
				.uri("http://www.kuaidi100.com/query?type=123&postid=123")
				.accept(MediaType.APPLICATION_JSON)
				.retrieve().bodyToMono(String.class);
				System.out.println(bodyMono.block());
	}
}